//
//  SPTagsTokenFieldDelegate.m
//  Sophie
//
//  Created by Anton Chebotaev on 22/10/09.
//  Copyright 2009 IFMO. All rights reserved.
//

#import "SPTagsManager.h"


@implementation SPTagsManager

@synthesize entry;

#pragma mark NSTextFieldDelegate
- (BOOL)tokenField:(NSTokenField *)tokenField hasMenuForRepresentedObject:(id)representedObject {
	return NO;
}

- (NSString *)tokenField:(NSTokenField *)tokenField editingStringForRepresentedObject:(id)representedObject {
	LJTag* tag = (LJTag*)representedObject;
	return [tag name];
}
- (NSString *)tokenField:(NSTokenField *)tokenField displayStringForRepresentedObject:(id)representedObject {
	LJTag* tag = (LJTag*)representedObject;
	return [tag name];
}

- (NSArray *)tokenField:(NSTokenField *)tokenField
completionsForSubstring:(NSString *)substring
		   indexOfToken:(NSInteger)tokenIndex
	indexOfSelectedItem:(NSInteger *)selectedIndex
{
	NSMutableArray *substitutions = [NSMutableArray array];
	for (LJTag *tag in [[entry journal] tags])
	{
		if ([[tag name] hasPrefix:substring]) {
			[substitutions addObject:[tag name]];
		}
	}
	return substitutions;		
}

- (id)tokenField:(NSTokenField *)tokenField representedObjectForEditingString:(NSString *)editingString {
	
	return [LJTag tagWithName:editingString];
}

@end
